#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e5+1000;
typedef long long LL;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
char str[maxn];
map<char,LL>map1[maxn];
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  LL t;cin>>t;
  while(t--){
    LL n,l;cin>>n>>l;
    for(LL i=0;i<n+10;i++) map1[i].clear();
    cin>>(str+1);
    LL len=0;
    if(l<=n/2)  len=__gcd(n,l);
    else len=n-l;
    for(LL i=1;i<=n;i++){
        map1[i%len][str[i]]++;
    }
    vector<char>v;
    for(LL i=0;i<len;i++){
        char ans;LL mx=0;
        for(auto j:map1[i]){
            if(mx<j.second){
                mx=j.second;
                ans=j.first;
            }
        }
        v.push_back(ans);
    }
    for(LL i=1;i<=n;i++){
        if(i%len==0) cout<<v[0];
        else cout<<v[i%len];
    }
    cout<<"\n";

  }
return 0;
}
